package com.idroc.coelacanth.core.dht;

import org.jboss.netty.buffer.ChannelBuffer;

import com.idroc.coelacanth.core.membership.Endpoint;
import com.idroc.coelacanth.core.message.AbstractMessage;

/**
 * 节点动作请求消息
 * @author yuyoo (yuyoo4j@163.com)
 * @date 2009-12-14 下午01:27:03
 */
public class NodeActionRequestMessage extends AbstractMessage {
	
	private Endpoint from = null;
	
	private int nodeNo = -1;

	@Override
	public void decode(ChannelBuffer ib) {

		nodeNo = ib.readInt();
		from = new Endpoint();
		from.setHost(decodeString(ib));
		from.setPort(ib.readInt());
	}

	@Override
	public void encode(ChannelBuffer ob) {

		ob.writeInt(nodeNo);
		encodeString(ob, from.getHost());
		ob.writeInt(from.getPort());
	}

	public Endpoint getFrom() {
		return from;
	}

	public void setFrom(Endpoint from) {
		this.from = from;
	}

	public int getNodeNo() {
		return nodeNo;
	}

	public void setNodeNo(int nodeNo) {
		this.nodeNo = nodeNo;
	}

	@Override
	public String toString() {

		StringBuilder sb = new StringBuilder();
		sb.append("NodeActionRequestMessage[");
		sb.append("nodeNo:").append(nodeNo).append(",");
		sb.append("from:").append(from);
		sb.append("]");
		return sb.toString();
	}
}
